module distlog
    parameter (max_file=30)
    integer, dimension(max_file) :: io
    integer :: nop
    integer :: op1
    data io / 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30/

contains 

subroutine init_distlog
    implicit none
    include "arrays.FI"
    include "pspec.FI"
    include "xsec.FI"
    integer :: ix, i
    character*80 filename
    ix = 1
!    do i=1, ng
!        write(filename, '(a,i01,a)'),'fluxMCPRG_G',i,'.txt'
!        open(io(ix), file=filename, status='unknown')
!        ix = ix + 1
!    end do
!    open(io(ix), file='fisMCPRG.txt', status='unknown')
!    ix = ix + 1

!    do i=1, ng
!        write(filename, '(a,i01,a)'),'fluxCMFD_G',i,'.txt'
!        open(io(ix), file=filename, status='unknown')
!        ix = ix + 1
!    end do
!    open(io(ix), file='fisCMFD.txt', status='unknown')
!    ix = ix + 1

!    do i=1, ng
!        write(filename, '(a,i01,a)'),'TRKL_G',i,'ac.txt'
!        open(io(ix), file=filename, status='unknown')
!        ix = ix + 1
!    end do
!    open(io(ix), file='fisMCac.txt', status='unknown')
!    op1 = ix
    do i=1, ng
        write(filename, '(a,i01,a)'),'fluxMC_G',i,'.txt'
        open(io(i), file=filename, status='unknown')
    end do
!    open(io(ix), file='fisMC.txt', status='unknown')
!    
!    nop = ix
end subroutine

! before CMFD
subroutine write_distlog1
    include "arrays.FI"
    include "pspec.FI"
    include "xsec.FI"
    integer :: g, i, j
    do g=1, ng
        write(io(g),'(2000(e11.5,1x))') (phi_fnm(i, g),i=1,totmesh)
    end do
!    write(io(op1+ng), '(2000(e11.5,1x))') (psi_f(i),i=1,totmesh)
end subroutine

! after CMFD
subroutine write_distlog2
    include "arrays.FI"
    include "pspec.FI"
    include "xsec.FI"
    include "hmgz.FI"
    integer :: g, i, j
!    do g=1, ng
!        write(io(g),'(2000(e11.5,1x))') (phi_f(i, g),i=1,totmesh)
!    end do
!    write(io(ng+1), '(2000(e11.5,1x))') (psi_f(i),i=1,totmesh)
!    do g=1, ng
!        write(io(ng+g),'(2000(e11.5,1x))') (hphi(i, g),i=1,totm_cmfd)
!    end do
!    write(io((ng+1)*2), '(2000(e11.5,1x))') (psi_c(i),i=1,totm_cmfd)
!    do g=1, ng
!        write(io((ng+1)*2+g),'(2000(e11.5,1x))') (trk_estT(i, g),i=1,totmesh)
!    end do
end subroutine

!subroutine write_distlog3
!    include "arrays.FI"
!    include "pspec.FI"
!    include "xsec.FI"
!    
!    
!endsubroutine
! write flux after prolongation
subroutine writeflux_prln
    
endsubroutine

subroutine finalize_distlog
    implicit none
    integer :: i
!    do i=1, nop
!        close(io(i))
!    end do
end subroutine

end module
